/* module.c */
#include <iostream>
#include <stdio.h>
#include "../include/libcellex.h"

//#include "Mesh/Mesh.h"
//#include "Mesh/_Func_.h"

#include "Mesh_IO/Mesh_IO.h"
#include "Mesh/libcell_iterator.h"

off_info::off_info()
{
	vertices = 0;
	v_info = nullptr;
	faces = 0;
	edges = 0;
	f_info = nullptr;
}


#define _ReadOff_ LibCell_ReadOff_

// 全局唯一的mesh对象
Mesh global_m;
off_info* global_info = nullptr;

// 初始化
int libcellex_init(off_info** _in)
{
	Mesh_init(&global_m);	//不能传入指针
	//_ReadOff_(&global_m, "t1.off", 3);

	*_in = new off_info;
	global_info = *_in;


	return 0;
}

int read_off(char* file)
{
	_ReadOff_(&global_m, file, 3);

	if (global_info)
	{
		//global_info->edges = global_m.faces(&global_m);
		int vertice_size = global_m.num_v(&global_m);
		global_info->vertices = vertice_size;

		global_info->edges = global_m.num_f(&global_m);
		global_info->faces = global_m.num_c(&global_m);
		int faces = global_info->faces;

		// 按顶点数申请
		global_info->v_info = new vertice_info[vertice_size]();
		auto pvl = global_info->v_info;

		int index = 0;
		for (auto vit = global_m.v_begin(&global_m); vit != global_m.v_end(&global_m); ++vit)
		{
			auto pv = *vit;

			// id
			pvl[index].id = pv.id;
			// point
			memcpy(pvl[index].point, pv.point, sizeof(decltype(*pv.point)) * vertice_size);

			// index++
			index++;

			//test
			auto at2 = pvl[index].point;
			int n(0);
		}

		// 按面数申请行数据
		global_info->f_info = new face_info[faces];
		auto pfinfo = global_info->f_info;

		int f_index(0);
		// 遍历
		for (auto cit = global_m.c_begin(&global_m); cit != global_m.c_end(&global_m); ++cit)
		{
			pfinfo[f_index].id = (*cit).id;

			// 面顶点数
			int _vertice_size = (*cit).vertices_size;
			pfinfo[f_index].vertice_size = _vertice_size;

			// 顶点序号
			pfinfo[f_index].vertice_index = new int[_vertice_size];

			int vindex(0);
			for (auto cvit = global_m.cv_begin(&global_m, *cit); cvit != global_m.cv_end(&global_m, *cit); ++cvit)
			{
				pfinfo[f_index].vertice_index[vindex] = (*cvit).id;
				vindex++;
				int n(0);
			}

			f_index++;
			int m(-1);
		}
	}
	return 0;
}

int libcellex_free(off_info* p)
{
	if (p)
	{
		delete p;
		p = nullptr;
	}

	Mesh_free(&global_m);
	//global_m = nullptr;

	return 0;
}


off_info::~off_info()
{
	if (v_info)
	{
		delete[] v_info;
		v_info = nullptr;
	}

	if (f_info)
	{
		delete[] f_info;
		f_info = nullptr;
	}
}

face_info::face_info()
{
	vertice_index = nullptr;
	vertice_size = 0;
}

face_info::~face_info()
{
	int n(0);	//这里释放有问题
	if (nullptr != vertice_index)
	{
		delete[] vertice_index;
		vertice_index = nullptr;
#ifdef DEBUG_INFO
		printf("~face_inf()\n");
#endif
	}
}



int func5_struct(off_info** _in_out)
{
	if (*_in_out)
	{
		delete* _in_out;
		*_in_out = nullptr;
	}
	// C++20 新特性
	////*_in_out = new off_info{ .edges = 1 };

	//*_in_out = new off_info;

	//auto p = *_in_out;
	////p->vertices_list = new double[4 * 3] { 0.0f };

	//p->f_info = new face_info[4];	//4行
	//p->f_info[0].vertice_index = new int[3]();	// test init
	//auto p2 = p->f_info[0].vertice_index;
	//p2[1] = p2[0] = 9;

	int n(-1);
	return 0;
}


int m_pr(int a, int b)
{
	//printf("this is module test \n");
	std::cout << "2024-02-27 11:07:50 dll print a= " << a << "; b=" << b << std::endl;

	return 0;
}

int func2_pointer(int* pa, int b)
{
	pa[0] = 134;
	return 0;
}

int func3_pointer(int* pa, int col, int row)
{
	pa[4] = 1407;	// 相当于1维数组，4代表修改了第1行0列的数据
	int colx = 1;
	//*((int*)pa + 2) = 1407;
	return 0;
}


vertice_info::vertice_info()
{
	point = new double[3]();
}

vertice_info::~vertice_info()
{
	if (point)
	{
		delete[] point;
		point = nullptr;
	}
}
